package com.hll.model;

import com.hll.util.PropertyUtil;

import java.util.HashMap;
import java.util.Map;

public class Pagination {

    /**配置文件里配置每页5条数据*/
    private static final String KEY_PAGE_SIZE = "pagination.pageSize";
    /**总数据数*/
    private int totalCount;

    private int pageSize;
    private int pageCount;
    private int currentPage;
    private int offset;

    private Map<String, Object> parameterMap = new HashMap<>();

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public boolean isFirstPage() {
        if (this.currentPage <= 1) {
            return true;
        }
        return false;
    }

    public boolean isLastPage() {
        if (this.currentPage >= this.getPageCount()) {
            return true;
        }
        return false;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageSize() {
        if (pageSize == 0) {
            /**搜索此属性列表中指定键的属性值*/
            String size = PropertyUtil.getProperty(KEY_PAGE_SIZE);
            pageSize = Integer.parseInt(size);
        }
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageCount() {
        if (totalCount < 1) {
            pageCount = 0;
        }
        pageCount = (totalCount - 1) / getPageSize() + 1;
        return pageCount;
    }

    public int getCurrentPage() {
        if (currentPage < 1) {
            currentPage = 1;
        }
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    /**输入的页数跳转*/
    public int getOffset() {
        offset = (getCurrentPage() - 1) * getPageSize();
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }
}